/**
 iTextUploader
 (C) 2008 Asgl (myselftdev@gmail.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.*/

/* Revision: $Id: PreferencesPanel.m 12 2008-07-25 10:05:51Z myselftdev $ */

#import "PreferencesPanel.h"

@implementation PreferencesPanel
- (id)initWithFrame:(CGRect)rect
{
    if ((self == [ super initWithFrame: rect ]) != nil)
    {
        short i;

        for(i=0;i<CELLS;i++)
        {
            cells[i] = nil;
        }

        [ self setDataSource: self ];
        [ self setDelegate: self ];
        
        dataDict = nil;
        dataFilename = @"";
    }

    return self;
}

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)aTable {

    /* Number of logical groups, including labels */
    return 1;
}

 - (int)preferencesTable:(UIPreferencesTable *)aTable
    numberOfRowsInGroup:(int)group
{
	return 2;
}

- (UIPreferencesTableCell *)preferencesTable:
    (UIPreferencesTable *)aTable
    cellForGroup:(int)group
{
     UIPreferencesTableCell * groupcell = [ [ UIPreferencesTableCell alloc ] init ];
     [groupcell autorelease];
     [ groupcell setTitle: @"Login Data" ];
     return groupcell;
}

- (float)preferencesTable:(UIPreferencesTable *)aTable
    heightForRow:(int)row
    inGroup:(int)group
    withProposedHeight:(float)proposed
{
    /* Return height for group titles */
    if (row == -1)
    {
            return 40;
    }
    return proposed;
}

- (BOOL)preferencesTable:(UIPreferencesTable *)aTable
    isLabelGroup:(int)group
{
    return NO;
}

- (UIPreferencesTableCell *)preferencesTable:
    (UIPreferencesTable *)aTable
    cellForRow:(int)row
    inGroup:(int)group
{
    UIPreferencesTextTableCell *cell;

    if (cells[row] != nil)
        return cells[row];

            switch (row) {
                case (0):
                    cell = [ [ UIPreferencesTextTableCell alloc ]
                        init ];
                    [ cell setTitle: @"Username" ];
                    [ cell setValue: [dataDict objectForKey:@"username"] ];
                    break;
                 case (1):
                    cell = [ [ UIPreferencesTextTableCell alloc ]
                        init ];
                    [ cell setTitle: @"Password" ];
                    [ cell setValue: [dataDict objectForKey:@"password"] ];
                    //[ [cell textField] setSecure: YES];
                    break;
           }
     
	[cell setEnabled: YES ];
	/*[[cell textField] setPreferredKeyboardType:0];
    [[cell textField] setInitialSelectionBehavior:2];
    [[cell textField] setAutoCapsType:0];
    [[cell textField] setAutoCorrectionType: 1];*/
    [ cell setShowSelection: NO ];
    cells[row] = cell;
    return cell;
}

- (void) setDataFilename: (NSString *)filename
{
	[dataFilename autorelease];
	dataFilename = [filename retain];
}

- (void) loadData
{
	if([dataFilename isEqualToString: @""])
	{
		return;
	}
	
	dataDict = [[NSMutableDictionary alloc] initWithContentsOfFile: dataFilename];
}

- (void) saveData;
{
	[dataDict setValue:[ cells[0] value] forKey:@"username"];
	[dataDict setValue:[ cells[1] value] forKey:@"password"];
	[dataDict writeToFile:dataFilename atomically:YES];
}

- (NSString *) username;
{
	return [dataDict objectForKey:@"username"];
}

- (NSString *) password;
{
	return [dataDict objectForKey:@"password"];
}

- (void) dealloc
{
	[dataDict release];
	[dataFilename release];
	
	short i;
	for(i=0;i<CELLS;i++)
	{
        [cells[i] release];
    }
    
	[self dealloc];
	[super dealloc];
}
@end